package com.kxc.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.kxc.dao.HisDataDao;
import com.kxc.entity.HisData;
/**
 * 测点实时数据业务类
 */
@Service
public class HisDataService {

	@Autowired
	private HisDataDao hisDataDao;

	public HisData add(HisData realData) {
		return hisDataDao.save(realData);
	}

	public HisData update(HisData realData) {
		return hisDataDao.save(realData);
	}


	public HisData getByPointNum(String pointNum) {
		return hisDataDao.findByPointNum(pointNum);
	}

	/**干滩实时数据
	 * @return
	 */
	public List<Map<String, Object>> getDryBeach() {
		//{"data":{"dBLength":"2","dblState":"YELLOW","safeSuperHigh":"23","sshState":"NORMAL","beachTopHeight":"12","slopeRatio":"2"}}
		List<HisData> realDatas =hisDataDao. findByMoniItem("DryBeach");
		List<Map<String, Object>>results=new ArrayList<>();
		for (HisData realData : realDatas) {
			String dataStr = realData.getData();
			@SuppressWarnings("unchecked")
			Map<String, Map<String, String>> dataJsonObj=(Map<String, Map<String, String>>) JSON.parse(dataStr);
			Map<String, String> data=dataJsonObj.get("data");
			Map<String, Object>result=new HashMap<>();
			result.put("pointNum", realData.getPointNum());
			result.put("dBLength", data.get("dBLength"));
			result.put("dblState", data.get("dblState"));
			result.put("safeSuperHigh", data.get("safeSuperHigh"));
			result.put("sshState", data.get("sshState"));
			result.put("beachTopHeight", data.get("beachTopHeight"));
			result.put("slopeRatio", data.get("slopeRatio"));
			result.put("collectTime", realData.getCollectTime());
			results.add(result);
		}
		return results;
	}

	/**浸润线实时数据
	 * @return
	 */
	public List<Map<String, Object>> getPhreaticLine() {
		//{"data":{"buriedDepth":"13","waterElevation":"22","state":"红色报警"}}
		List<HisData> realDatas =hisDataDao. findByMoniItem("PhreaticLine");
		List<Map<String, Object>>results=new ArrayList<>();
		for (HisData realData : realDatas) {
			String dataStr = realData.getData();
			@SuppressWarnings("unchecked")
			Map<String, Map<String, String>> dataJsonObj=(Map<String, Map<String, String>>) JSON.parse(dataStr);
			Map<String, String> data=dataJsonObj.get("data");
			Map<String, Object>result=new HashMap<>();
			result.put("pointNum", realData.getPointNum());
			result.put("buriedDepth", data.get("buriedDepth"));
			result.put("waterElevation", data.get("waterElevation"));
			result.put("state", data.get("state"));
			result.put("collectTime", realData.getCollectTime());
			results.add(result);
		}
		return results;
	}
	/**
	 * 获取降雨量实时数据
	 * */
	public List<Map<String, Object>> getRainfall() {
		//{"data":[{"rainfall":"0","state":"YELLOW","unit":"1h"},{"rainfall":"0.1","state":"NORMAL","unit":"3h"},{"rainfall":"0","state":"RED","unit":"6h"},{"rainfall":"0","state":"RED","unit":"12h"},{"rainfall":"0","state":"BLUE","unit":"24h"}]}
		List<HisData>  realDatas=hisDataDao. findByMoniItem("Rainfall");
		List<Map<String, Object>>results=new ArrayList<>();
		for (HisData realData : realDatas) {
			String dataStr = realData.getData();
			@SuppressWarnings("unchecked")
			Map<String, List<Map<String, String>>> dataJsonObj=(Map<String, List<Map<String, String>>>) JSON.parse(dataStr);
			List<Map<String, String>> datas=dataJsonObj.get("data");
			for(Map<String, String> data :datas){
				Map<String, Object>result=new HashMap<>();
				result.put("pointNum", realData.getPointNum());
				result.put("unit", data.get("unit"));
				result.put("rainfall", data.get("rainfall"));
				result.put("state", data.get("state"));
				result.put("collectTime", realData.getCollectTime());
				results.add(result);
			}
		}
		return results;
	}

	/**
	 * @return
	 */
	public List<HisData> getSurfaceDisplacement() {
		return hisDataDao. findByMoniItem("SurfaceDisplacement");
	}

	/**库水位
	 * @return
	 */
	public List<Map<String, Object>> getReservoirWaterLevel() {
		//{"data":{"height": "12","state":"NORMAL"}}
		List<HisData> realDatas =hisDataDao. findByMoniItem("ReservoirWaterLevel");
		List<Map<String, Object>>results=new ArrayList<>();
		for (HisData realData : realDatas) {
			String dataStr = realData.getData();
			@SuppressWarnings("unchecked")
			Map<String, Map<String, String>> dataJsonObj=(Map<String, Map<String, String>>) JSON.parse(dataStr);
			Map<String, String> data=dataJsonObj.get("data");
			Map<String, Object>result=new HashMap<>();
			result.put("pointNum", realData.getPointNum());
			result.put("height", data.get("height"));
			result.put("state", data.get("state"));
			result.put("collectTime", realData.getCollectTime());
			results.add(result);
		}
		return results;
	}

	public List<HisData> getInternalDisplacement() {
		return hisDataDao.findByMoniItem("InternalDisplacement");

	}
	public List<HisData> find(String moniItem, String pointNum, String state, String beginDate, String endDate) {
		List<HisData> resultList = null;
		Specification<HisData> querySpecifi = new Specification<HisData>() {
			@Override
			public Predicate toPredicate(Root<HisData> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder cb) {
				
				List<Predicate> predicates = new ArrayList<>();
				if (beginDate!=null&&!"".equals(beginDate)) {
					//大于或等于传入时间
					predicates.add(cb.greaterThanOrEqualTo(root.get("collectTime").as(String.class), beginDate));
				}
				if (endDate!=null&&!"".equals(endDate)) {
					//小于或等于传入时间
					predicates.add(cb.lessThanOrEqualTo(root.get("collectTime").as(String.class), endDate));
				}
				if (moniItem!=null&&!"".equals(moniItem)) {
					predicates.add(cb.equal(root.get("moniItem").as(String.class), moniItem));
				}
				if (pointNum!=null&&!"".equals(pointNum)) {
					predicates.add(cb.equal(root.get("pointNum").as(String.class), pointNum));
				}
				// and到一起的话所有条件就是且关系，or就是或关系
				return cb.and(predicates.toArray(new Predicate[predicates.size()]));
			}
		};
		resultList = hisDataDao.findAll(querySpecifi);
		return resultList;
	}


}
